package com.jiai.cloud.api;




import com.jiai.cloud.base.util.ValidateCode;
import com.jiai.cloud.feign.GoldAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Created by Administrator on 2017/5/25.
 */
@RestController
@RequestMapping("/gold")
public class GoldController {

    @Autowired
    private GoldAPI goldAPI;

    //邮箱验证码验证
    @RequestMapping(value = "/chekcEmail",produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public Object chekcEmail(@RequestParam("email") String email,@RequestParam("code") String code){
        return goldAPI.chekcEmail(email,code);

    }
    //邮箱发送
    @RequestMapping(value = "/sendEmailCode",produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
    public  Object sendEmail(@RequestParam("email") String email) {
      return  goldAPI.sendEmail(email);
     }
    /**
     * 响应验证码页面
     * @return
     */
    @RequestMapping(value="/validateCode" ,method=RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public String validateCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        HttpSession session = request.getSession();

        ValidateCode vCode = new ValidateCode(120,40,5,100);
        session.setAttribute("code", vCode.getCode());
        vCode.write(response.getOutputStream());
        return null;
    }

}
